import { Component } from "@angular/core";
import { SFSchema, SFUISchema } from "@delon/form";
import { _HttpClient } from "@delon/theme";
import { NzMessageService } from "ng-zorro-antd/message";
import { NzModalRef } from "ng-zorro-antd/modal";
import { Api } from "src/app/shared/api";
import { ResponseCode } from "src/app/shared/response.code";

@Component({
    selector: 'app-dashboard-share-folder',
    templateUrl: './share-folder.component.html',
})
export class ShareFolderComponent{
    record: any = {};
    parentId: string='0';
    schema: SFSchema = {
        properties: {
            name: {
                type: 'string',
                title: '名称',
            },
        },
    };
    ui: SFUISchema = {
        '*': {
            spanLabel: 2,
            spanControl: 22
        },
    };

    constructor(
        private modal: NzModalRef,
        private msgSrv: NzMessageService,
        public http: _HttpClient,
    ) {
        const config = this.modal.getConfig();
        if(config&&config.nzComponentParams){
            this.parentId = config.nzComponentParams?.parentId;
            if(!this.parentId){
                this.parentId = '0';
            }
        }
    }

    close() {
        this.modal.destroy();
    }

    save(value: any) {
        console.log(value);
        let entity = {name:value.name,userAuthority:0,groupAuthority:0,otherAuthority:0,parentId:null};
        if(value.user){
            value.user.forEach(element => {
                entity.userAuthority += element;
            });
        }else{
            entity.userAuthority = 7;
        }
        if(value.group){
            value.group.forEach(element => {
                entity.groupAuthority += element;
            });
        }
        if(value.other){
            value.other.forEach(element => {
                entity.otherAuthority += element;
            });
        }
        let that = this;
        if(this.record.id){
            delete entity.parentId;
            this.http.put(Api.BaseNodeApi+'/share/folder',entity)
            .subscribe(res=>{
                if(res.code===ResponseCode.SUCCESS){
                    that.msgSrv.success("成功！");
                    this.modal.close(true);
                }else{
                    that.msgSrv.error(res.message?res.message:"失败，未知原因！");
                }
            })
        }else{
            entity.parentId=this.parentId;
            this.http.post(Api.BaseNodeApi+'/share/folder',entity)
            .subscribe(res=>{
                if(res.code===ResponseCode.SUCCESS){
                    that.msgSrv.success("成功！");
                    this.modal.close(true);
                }else{
                    that.msgSrv.error(res.message?res.message:"失败，未知原因！");
                }
            })
        }       
    }
}